import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;

public class MusicManager {
    private static MediaPlayer menuMusicPlayer;
    private static MediaPlayer gameMusicPlayer;
    private static double currentVolume = 0.5; // 默认音量

    public static void initialize() {
        // 预加载音乐文件
        Media menuMusic = new Media(MusicManager.class.getResource(GameConstants.MAIN_MENU_MUSIC).toString());
        Media gameMusic = new Media(MusicManager.class.getResource(GameConstants.GAME_MUSIC).toString());

        menuMusicPlayer = new MediaPlayer(menuMusic);
        gameMusicPlayer = new MediaPlayer(gameMusic);

        // 设置游戏音乐循环播放
        gameMusicPlayer.setCycleCount(MediaPlayer.INDEFINITE);

        // 设置初始音量
        menuMusicPlayer.setVolume(currentVolume);
        gameMusicPlayer.setVolume(currentVolume);
    }

    public static double getVolume() {
        return currentVolume;
    }

    public static void setVolume(double volume) {
        currentVolume = volume;
        if (menuMusicPlayer != null) {
            menuMusicPlayer.setVolume(volume);
        }
        if (gameMusicPlayer != null) {
            gameMusicPlayer.setVolume(volume);
        }
    }

    public static void playMenuMusic() {
        stopGameMusic();
        menuMusicPlayer.play();
    }

    public static void playGameMusic() {
        stopMenuMusic();
        gameMusicPlayer.play();
    }

    public static void stopMenuMusic() {
        if (menuMusicPlayer != null) {
            menuMusicPlayer.stop();
        }
    }

    public static void stopGameMusic() {
        if (gameMusicPlayer != null) {
            gameMusicPlayer.stop();
        }
    }

}